package com.QSystem.service;

import com.QSystem.mapper.UserMapper;
import com.QSystem.pojo.User;
import com.QSystem.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    User temp=new User();
    Map<String,Object> map=new HashMap<>();


    //查询数据库中是否已经存在该用户,成功返回数据
    public Result queryByName(User user){

        temp=userMapper.queryByName(user);
        if(temp!=null&&(temp.getPwd().equals(user.getPwd()))) {
            map.put("account",temp);
            return Result.ok().data(map);
        }
        else if(temp==null){
            return Result.error().message("没有该用户");
        }
        return Result.error().message("密码不正确");
    }


    //注册用户
    public Result register(User user){
        temp=userMapper.queryByName(user);
        if(temp==null){
            userMapper.addUser(user);
        }
        else {
            return Result.error().message("该账号已存在!");
        }
        return Result.ok().message("注册成功!");
    }


    //根据用户名字获取id
    public Integer getUserId(String userName){
        return userMapper.getUserId(userName);
    }


    //获取用户
    public User getbyName(User user){
        return userMapper.query(user);
    }

    public boolean addUser(User user){
        userMapper.addUser(user);
        return false;
    }

    public boolean updateUser(User user){
        userMapper.updateUser(user);
        return  false;
    }

    public boolean deleteUser(User user){

        userMapper.deleteUser(1);
        return false;
    }

}
